const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGoodList();
  },
  getGoodList:function(){
    var that = this;
    api.POST({
      url: app.globalData.base_url + "goods/getAll",
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            list: res.data.data.goods_list
          })
        } else {
          util.showError(res.data.message);
        }
      }
    })
  },
  //商品勾选
  selected:function(e){
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    var data = this.data.list;
    if (data[index].selected==0){
      data[index].selected = 1
    }else{
      data[index].selected = 0
    }
    this.setData({
      list:data
    })
  },
  //提交
  submit:function(){
    var data = this.data.list;
    var record = [];
    for(var i=0;i<data.length;i++){
      if(data[i].selected==1){
        record.push(data[i].sku_code)
      }
    }

    var record = JSON.stringify(record)
    api.POST({
      params: { record: record},
      url: app.globalData.base_url + "inn/saveGoodsList",
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          util.showError(res.data.message);
        }
      }
    })


  }
})